// 递归法

import { TreeNode } from '../types'

const root = {
	val: 'A',
	left: {
		val: 'B',
		left: {
			val: 'D'
		},
		right: {
			val: 'E'
		}
	},
	right: {
		val: 'C',
		right: {
			val: 'F'
		}
	}
}

// 先序遍历
function preOrder(root: TreeNode | undefined): void {
	if (!root) {
		return
	}

	console.log(root.val)

	preOrder(root.left)
	preOrder(root.right)
}

preOrder(root) //A B D E C F

// 中序遍历
function inOrder(root: TreeNode | undefined): void {
	if (!root) {
		return
	}

	inOrder(root.left)
	console.log(root.val)
	inOrder(root.right)
}

inOrder(root) // D B E A C F

// 后序遍历
function postOrder(root: TreeNode | undefined): void {
	if (!root) {
		return
	}

	postOrder(root.left)
	postOrder(root.right)
	console.log(root.val)
}

postOrder(root) // D E B F C A
